package com.example.tongue.ui.Online.Body;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tongue.ui.Online.Body.utils.ScoreCalculationSeven;
import com.example.tongue.R;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class GasSilt extends Activity implements View.OnClickListener {
    private Button next8,whetherGasSilt;
    private RadioGroup lowMood,nervous,
                        sentimentality,frightened,
                        flanks,sigh,
                        foreignBodySensation;




    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gassilt);

        //绑定控件资源
        next8=findViewById(R.id.Next8);

                lowMood=findViewById(R.id.LowMood);
                nervous=findViewById(R.id.Nervous);
                sentimentality=findViewById(R.id.Sentimentality);
                frightened=findViewById(R.id.Frightened);
                flanks=findViewById(R.id.Flanks);
                sigh=findViewById(R.id.Sigh);
                foreignBodySensation=findViewById(R.id.ForeignBodySensation);


        //设置监听器
        next8.setOnClickListener(this);

                lowMood.setOnClickListener(this);
                nervous.setOnClickListener(this);
                sentimentality.setOnClickListener(this);
                frightened.setOnClickListener(this);
                flanks.setOnClickListener(this);
                sigh.setOnClickListener(this);
                foreignBodySensation.setOnClickListener(this);


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Next8:{
                //计算结果,把结果写入缓存
                GasSiltResult();

//                Toast.makeText(GasSilt.this, "进入阴虚质", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(GasSilt.this, YinDeficiency.class));

            }
            break;

        }
    }

    public void GasSiltResult(){
        int lowMoodid= lowMood.getCheckedRadioButtonId();
        RadioButton lowMoodbtn= findViewById(lowMoodid);
        String lowMoodfin=lowMoodbtn.getText().toString();

        int  nervousid= nervous.getCheckedRadioButtonId();
        RadioButton nervousbtn= findViewById(nervousid);
        String nervousfin=nervousbtn.getText().toString();

        int sentimentalityid=  sentimentality.getCheckedRadioButtonId();
        RadioButton sentimentalitybtn= findViewById(sentimentalityid);
        String sentimentalityfin=sentimentalitybtn.getText().toString();

        int  frightenedid= frightened.getCheckedRadioButtonId();
        RadioButton  frightenedbtn= findViewById( frightenedid);
        String  frightenedfin= frightenedbtn.getText().toString();

        int  flanksid= flanks.getCheckedRadioButtonId();
        RadioButton  flanksbtn= findViewById( flanksid);
        String  flanksfin= flanksbtn.getText().toString();


        int  sighid= sigh.getCheckedRadioButtonId();
        RadioButton  sighbtn= findViewById( sighid);
        String  sighfin= sighbtn.getText().toString();

        int foreignBodySensationid= foreignBodySensation.getCheckedRadioButtonId();
        RadioButton foreignBodySensationbtn= findViewById(foreignBodySensationid);
        String foreignBodySensationfin=foreignBodySensationbtn.getText().toString();




        Map<String,Integer>  lowMoodMap=new HashMap<String,Integer>();
        lowMoodMap.put("没有",1);
        lowMoodMap.put("很少",2);
        lowMoodMap.put("有时",3);
        lowMoodMap.put("常常",4);
        lowMoodMap.put("经常",5);

        Map<String,Integer> nervousMap=new HashMap<String,Integer>();
        nervousMap.put("没有",1);
        nervousMap.put("很少",2);
        nervousMap.put("有时",3);
        nervousMap.put("常常",4);
        nervousMap.put("经常",5);

        Map<String,Integer>    sentimentalityMap=new HashMap<String,Integer>();
        sentimentalityMap.put("没有",1);
        sentimentalityMap.put("很少",2);
        sentimentalityMap.put("有时",3);
        sentimentalityMap.put("常常",4);
        sentimentalityMap.put("经常",5);

        Map<String,Integer>  frightenedMap=new HashMap<String,Integer>();
        frightenedMap.put("没有",1);
        frightenedMap.put("很少",2);
        frightenedMap.put("有时",3);
        frightenedMap.put("常常",4);
        frightenedMap.put("经常",5);

        Map<String,Integer> flanksMap=new HashMap<String,Integer>();
        flanksMap.put("没有",1);
        flanksMap.put("很少",2);
        flanksMap.put("有时",3);
        flanksMap.put("常常",4);
        flanksMap.put("经常",5);

        Map<String,Integer>  sighMap=new HashMap<String,Integer>();
        sighMap.put("没有",1);
        sighMap.put("很少",2);
        sighMap.put("有时",3);
        sighMap.put("常常",4);
        sighMap.put("经常",5);

        Map<String,Integer> foreignBodySensationMap=new HashMap<String,Integer>();
        foreignBodySensationMap.put("没有",1);
        foreignBodySensationMap.put("很少",2);
        foreignBodySensationMap.put("有时",3);
        foreignBodySensationMap.put("常常",4);
        foreignBodySensationMap.put("经常",5);





        Integer gasSlitResult = lowMoodMap.get(lowMoodfin)+
                nervousMap.get(nervousfin) +
                sentimentalityMap.get(sentimentalityfin) +
                frightenedMap.get(frightenedfin)+
                flanksMap.get(flanksfin)+
                sighMap.get(sighfin)+
                foreignBodySensationMap.get(  foreignBodySensationfin)
                ;
        ScoreCalculationSeven scoreCalculationSeven=new ScoreCalculationSeven(gasSlitResult);
        String ConversionScore=scoreCalculationSeven.getResult();

        TextView gasSiltResult=(TextView)findViewById(R.id.GasSiltResult);
        gasSiltResult.setText("气郁质结果:"+ConversionScore);

        File dataFileDir=new File(getExternalCacheDir(),"PhysiqueDataFile");
        FileWriter writer=null;
        try {
            //开启写入流
            writer=new FileWriter(dataFileDir+"/PhysiqueData.txt",true);
            writer.write(ConversionScore+"\n");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                System.out.println("数据文件已生成");
            }
        }


    }

}
